Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Windows
Imports Infragistics.Windows.Chart
Imports System.Windows.Controls
Imports System.ComponentModel
Imports Microsoft.Practices.Prism.Commands

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<Serializable()> _
Public Class VMDashboardDefinitionZone
    Inherits VMSectionBase
    Implements ICloneable


    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Name As String

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Selected As Boolean

    Private _StartColumn As Integer = 0
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property StartColumn As Integer
        Get
            Return _StartColumn
        End Get
        Set(ByVal value As Integer)
            OnPropertyChanging("StartColumn")
            _StartColumn = value
            OnPropertyChanged("StartColumn")
        End Set
    End Property

    Private _StartRow As Integer = 0
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property StartRow As Integer
        Get
            Return _StartRow
        End Get
        Set(ByVal value As Integer)
            OnPropertyChanging("StartRow")
            _StartRow = value
            OnPropertyChanged("StartRow")
        End Set
    End Property

    Private _ColumnCount As Integer = 1
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property ColumnCount As Integer
        Get
            Return _ColumnCount
        End Get
        Set(ByVal value As Integer)
            OnPropertyChanging("ColumnCount")
            _ColumnCount = value
            OnPropertyChanged("ColumnCount")
        End Set
    End Property

    Private _RowCount As Integer = 1
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property RowCount As Integer
        Get
            Return _RowCount
        End Get
        Set(ByVal value As Integer)
            OnPropertyChanging("RowCount")
            _RowCount = value
            OnPropertyChanged("RowCount")
        End Set
    End Property

    Private zprov As Services.ZoneProviderService
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property ZoneTypes As IEnumerable
        Get
            If zprov Is Nothing Then
                zprov = TryCast(ModuleIndicateurs.AppInstance.GetService(Of Services.IZoneProviderService)(),  _
                      Services.ZoneProviderService)

            End If
            Return If(zprov Is Nothing, Nothing, zprov.ZoneTypes)
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property MaxColumns As Integer = 1
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property MaxRows As Integer = 1

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property Validated As Boolean

    Private WithEvents _GraphDefinition As New VMZoneGraphDefinition
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property GraphDefinition As VMZoneGraphDefinition
        Get
            Return _GraphDefinition
        End Get
        Set(ByVal value As VMZoneGraphDefinition)
            OnPropertyChanging("GraphDefinition")
            _GraphDefinition = value
            OnPropertyChanged("GraphDefinition")
        End Set
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub _GraphDefinition_LayoutChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles _GraphDefinition.LayoutChange
        OnPropertyChanged("GraphDefinition")
    End Sub

    Private _SelectedZoneType As ZoneType
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Property SelectedZoneType As ZoneType
        Get
            Return _SelectedZoneType
        End Get
        Set(ByVal value As ZoneType)
            OnPropertyChanging("SelectedZoneType")
            _SelectedZoneType = value
            OnPropertyChanged("SelectedZoneType")
        End Set
    End Property

    Private _EditCommand As New DelegateCommand(Of Object)(AddressOf EditExecute, AddressOf EditCanExecute)
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property EditCommand As DelegateCommand(Of Object)
        Get
            Return _EditCommand
        End Get
    End Property
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EditExecute(ByVal parameter As Object)
        Dim ui = ModuleIndicateurs.AppInstance.GetService(Of IUIService)()
        GraphDefinition.DetailEdit = New VMEditableObject(GraphDefinition.DetailInstance)
        ui.ShowMessage(GraphDefinition)
    End Sub
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Function EditCanExecute(ByVal parameter As Object) As Boolean
        Return True
    End Function

    Sub ReCalculate()
        GraphDefinition.ReCalculate()
    End Sub

    'Private _BorderColor As Media.Color = Media.Colors.AntiqueWhite
    'Public Property BorderColor As Media.Color
    '    Get
    '        Return _BorderColor
    '    End Get
    '    Set(ByVal value As Media.Color)
    '        OnPropertyChanging("BorderColor")
    '        _BorderColor = value
    '        OnPropertyChanged("BorderColor")
    '    End Set
    'End Property

    'Private _BorderSize As Double
    'Public Property BorderSize As Double
    '    Get
    '        Return _BorderSize
    '    End Get
    '    Set(ByVal value As Double)
    '        OnPropertyChanging("BorderSize")
    '        _BorderSize = value
    '        OnPropertyChanged("BorderSize")
    '    End Set
    'End Property

    'Private _CornerRadius As Double
    'Public Property CornerRadius As Double
    '    Get
    '        Return _CornerRadius
    '    End Get
    '    Set(ByVal value As Double)
    '        OnPropertyChanging("CornerRadius")
    '        _CornerRadius = value
    '        OnPropertyChanged("CornerRadius")
    '    End Set
    'End Property

    'Private _BackColor As Media.Color = Media.Colors.AliceBlue
    'Public Property BackColor As Media.Color
    '    Get
    '        Return _
    '        Return _BackColor

    '    End Get
    '    Set(ByVal value As Media.Color)
    '        OnPropertyChanging("BackColor")
    '        _BackColor = value
    '        OnPropertyChanged("BackColor")
    '    End Set
    'End Property

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return New VMDashboardDefinitionZone() With {
            .StartColumn = _StartColumn,
            .StartRow = _StartRow,
            .ColumnCount = _ColumnCount,
            .RowCount = _RowCount,
            .GraphDefinition = _GraphDefinition.Clone()}
    End Function

    Public Function ToXElement(ByVal options As Object) As XElement
        Dim grd = TryCast(options, Grid)
        Dim q = (From elt As Control In grd.Children
                      Where elt.DataContext Is Me
                      Select elt).FirstOrDefault()

        Dim editgrd = TryCast(q, EditZone)

        Dim editgrdfacade = New EditZoneFacade(editgrd)

        Dim xser = TryCast(Me.GraphDefinition.DetailInstance, IXelementSerializable)

        If xser Is Nothing Then
            xser = TryCast(Me.GraphDefinition.DetailEdit.target, IXelementSerializable)
        End If
        Dim x = <DashboardDefinitionZone
                    StartColumn=<%= StartColumn %>
                    StartRow=<%= StartRow %>
                    ColumnCount=<%= ColumnCount %>
                    RowCount=<%= RowCount %>
                    MaxColumns=<%= MaxColumns %>
                    MaxRows=<%= MaxRows %>>

                    <Layout>
                        <%= editgrdfacade.ToXElement(Nothing) %>
                    </Layout>
                    <Detail Type=<%= xser.GetType() %>>
                        <%= If(xser IsNot Nothing, xser.ToXElement(Nothing), Nothing) %>
                    </Detail>
                </DashboardDefinitionZone>

        Return x
    End Function

    Sub FromXElement(ByVal x As XElement)
        MaxColumns = x.@MaxColumns
        MaxRows = x.@MaxRows
        StartColumn = x.@StartColumn
        StartRow = x.@StartRow
        RowCount = x.@RowCount
        ColumnCount = x.@ColumnCount

        Dim ass = Me.GetType().Assembly

        Dim t = ass.GetType(x.<Detail>.@Type)
        Dim inst = Activator.CreateInstance(t)
        Dim facade = TryCast(inst, Facades.IFacade)
        If facade IsNot Nothing Then
            Dim xser = TryCast(inst, IXelementSerializable)
            If xser IsNot Nothing Then
                xser.FromXElement(Nothing, x.<Detail>.First().Elements().FirstOrDefault())
            End If
            GraphDefinition.DetailInstance = facade.Brother
            GraphDefinition.Detail = New VMEditableObject(facade)
        Else
            Dim xser = TryCast(inst, IXelementSerializable)
            If xser IsNot Nothing Then
                xser.FromXElement(Nothing, x.<Detail>.First().Elements().FirstOrDefault())
            End If
            GraphDefinition.DetailInstance = inst
        End If

        'Me.GraphDefinition.DetailInstance = New Zones.TextZone()
        prebuildeditzone = New EditZone()
        Grid.SetColumnSpan(prebuildeditzone, 2 * ColumnCount - 1)
        Grid.SetRow(prebuildeditzone, 2 * StartRow)
        Grid.SetColumn(prebuildeditzone, 2 * StartColumn)
        Grid.SetRowSpan(prebuildeditzone, 2 * RowCount - 1)

        Dim editgrdfacade = New EditZoneFacade(prebuildeditzone)
        editgrdfacade.FromXElement(x.<Layout>.<EditZone>.FirstOrDefault())

        prebuildeditzone.DataContext = Me
    End Sub

    Friend prebuildeditzone As EditZone
  

End Class

Public Interface IXelementSerializable

    Function ToXElement(ByVal options As Object) As XElement
    Sub FromXElement(ByVal options As Object, ByVal x As XElement)

End Interface
